﻿using System;
using System.Collections.Generic;
using System.Linq;
//using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BL;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;

namespace WebsiteBanHang
{
    public partial class _Default : System.Web.UI.Page
    {
        SanPhamBL sanPham = new SanPhamBL();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            List<SanPhamDAL> dsSanPham = new List<SanPhamDAL>();

            List<SanPhamDAL> dsSPKM = new List<SanPhamDAL>();

            List<SanPhamDAL> dsSPMoi = new List<SanPhamDAL>();

            /*
            //dsSanPham = sanPham.LayDanhSachSanPham();
            dsSanPham = sanPham.LayDSSanPhamTheoMaLoai("Loai4");
            listview_products.DataSource = dsSanPham;
            listview_products.DataBind();
            */            
            string lsp = Request.QueryString["MaLoaiSanPham"];
            if (lsp == null)
                dsSanPham = sanPham.LayDanhSachSanPham();
            else
                dsSanPham = sanPham.LayDSSanPhamTheoMaLoai(lsp);
            listview_products.DataSource = dsSanPham;
            listview_products.DataBind();

            dsSPKM = sanPham.LayDanhSachSanPhamKhuyenMai();
            List<SanPhamDAL> spkm = new List<SanPhamDAL>();
            if (dsSPKM.Count >= 5)
            {
                for (int i = 0; i < 5; ++i)
                {
                    spkm.Add(dsSPKM[i]);
                }
                Repeater1.DataSource = spkm;
                Repeater1.DataBind();
            }
            //else
            //{
            //    Repeater1.DataSource = dsSPKM;                
            //}
            //Repeater1.DataBind();
            //Repeater1.DataSource = dsSPKM;
            //Repeater1.DataBind();

            dsSPMoi = sanPham.LayDanhSachSanPhamMoi();

            List<SanPhamDAL> spm = new List<SanPhamDAL>();
            if (dsSPMoi.Count >= 5)
            {
                for (int i = 0; i < 5; ++i)
                {
                    spm.Add(dsSPMoi[i]);
                }
                Repeater2.DataSource = spm;
                Repeater2.DataBind();
            }
            
            //    Repeater1.DataSource = spm;
            //}
            //else
            //{
            //    Repeater1.DataSource = dsSPMoi;                
            //}            
            //Repeater2.DataBind();
        }
        //public static Stream ConvertBytesToImage(byte[] photo_aray)
        //{
        //    return new MemoryStream((byte[])photo_aray);
        //}

        //public Image byteArrayToImage(byte[] byteArrayIn)
        //{
        //    MemoryStream ms = new MemoryStream(byteArrayIn);
        //    //var newImage = System.Drawing.Image.FromStream(ms);
        //    //Image returnImage =Image.FromStream(ms);
        //    MemoryStream stream = new MemoryStream(byteArrayIn);
        //    var newImage = System.Drawing.Image.FromStream(stream);
        //    stream.Dispose(); 
        //}



        //public static Image ConvertBytesToImage(byte[] photo_aray)
        //{
        //    Image img = Image.FromStream(new MemoryStream(photo_aray));
        //    return img;
        //}

        /*
        private static string GetTempFolderName()
        {

            string strTempFolderName = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + @"\";



            if (Directory.Exists(strTempFolderName))
            {

                return strTempFolderName;

            }

            else
            {

                Directory.CreateDirectory(strTempFolderName);

                return strTempFolderName;

            }

        }

        private  void byteArrayToImage(byte[] byteArrayIn)
        {
            System.Drawing.Image newImage;
            string strFileName = GetTempFolderName() + "yourfilename.gif";

            if (byteArrayIn != null)
            {

                using (MemoryStream stream = new MemoryStream(byteArrayIn))
                {

                    newImage = System.Drawing.Image.FromStream(stream);



                    newImage.Save(strFileName);



                    img.Attributes.Add("src", strFileName);

                }



                lblMessage.Text = "The image conversion was successful.";

            }

            else
            {

                Response.Write("No image data found!");

            }
        }*/
        

    }
}
